package com.xyrl.project.app.controller.sales;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyrl.common.util.ObjectUtil;
import com.xyrl.common.util.RedisUtil;
import com.xyrl.common.util.StringUtil;
import com.xyrl.project.app.util.ShiroUtil;
import com.xyrl.project.common.base.vo.ResponseData;
import com.xyrl.project.common.constant.XyrlConstant;
import com.xyrl.project.common.csr.vo.CsrRegisterVo;
import com.xyrl.project.common.log.enu.SmsSendType;
import com.xyrl.project.common.store.entity.Store;
import com.xyrl.project.common.store.entity.StoreLegal;
import com.xyrl.project.common.store.enu.StoreStatus;
import com.xyrl.project.common.store.service.StoreAuthService;
import com.xyrl.project.common.store.service.StoreLegalService;
import com.xyrl.project.common.store.service.StoreService;
import com.xyrl.project.common.store.vo.StoreVo;
import com.xyrl.project.common.system.service.DictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;

/**
 *@className SaleCustomerController
 *@company 星纭融链科技服务有限公司
 *@author 徐恩源
 *@version 1.0
 *@date 2019-12-30 10:19:25
 *@Description 商户信息controller
 */
@Slf4j
@RestController
@RequestMapping("/sales/store")
@Api(tags = "商户模块")
public class SalesStoreController {
    @Autowired
    private StoreService storeService;
    @Autowired
    private StoreAuthService storeAuthService;
    @Autowired
    private StoreLegalService storeLegalService;
    @Autowired
    private DictionaryService dictionaryService;

    @ApiOperation(value = "注册step1-商户注册")
    @PostMapping("/register")
    public ResponseData register(@Valid CsrRegisterVo registerVo) {
        Store store = storeService.register(registerVo, ShiroUtil.loginUserId());
        String cacheKey = StringUtil.concat(XyrlConstant.ALIYUN_SMS_VALID_TERM, SmsSendType.registerCode.name(), ":", registerVo.getMobile());
        RedisUtil.delKey(cacheKey);
        return ResponseData.success(store.getId());
    }
    /**
     * 注册step2-身份认证
     * @author 徐恩源
     * @date 2020-01-06 16:17:42 
     * @param storeLegal
     */
    @ApiOperation(value = "注册step2-身份认证", notes = "附件module：storeAuth")
    @PostMapping("/idCardAuth")
    public ResponseData idCardAuth(@Valid StoreLegal storeLegal) {
        Long salesId = ShiroUtil.loginUserId();
        String result = storeLegalService.bindIdCard(storeLegal, salesId, true);
        if (ObjectUtil.isNotEmpty(result)) {
            return ResponseData.error(result);
        }
        return ResponseData.success();
    }
    /**
     * 注册step3-人脸认证
     * @author 徐恩源
     * @date 2020-01-06 16:17:53
     * @param storeId
     * @param attIds
     */
    @ApiOperation(value = "注册step3-人脸认证", notes = "附件module：storeAuth")
    @PostMapping("/faceAuth")
    public ResponseData faceAuth(Long storeId, Long[] attIds) {
        String result = storeAuthService.faceAuth(storeId, ShiroUtil.loginUserId(), Arrays.asList(attIds), true);
        if (ObjectUtil.isNotEmpty(result)) {
            return ResponseData.error(result);
        }

        return ResponseData.success();
    }

    /**
     * 商户基础资料
     * @author 徐恩源
     * @date 2020-01-06 16:40:48
     * @param storeId
     */
    @ApiOperation(value = "商户基础资料")
    @GetMapping("/get/{storeId}")
    public ResponseData<StoreVo> get(@PathVariable("storeId") Long storeId) {

        StoreVo storeVo = storeService.findStoreVoById(storeId);
        if (null == storeVo || !ShiroUtil.loginUserId().equals(storeVo.getSalesId())) {
            return ResponseData.error("商户不存在");
        }
        storeVo.setIndustryName(dictionaryService.findValueByKey(storeVo.getIndustry()));
        return ResponseData.success(storeVo);
    }

    /**
     * 修改商户基础资料
     * @author 徐恩源
     * @date 2020-01-06 16:40:57
     * @param storeVo
     */
    @ApiOperation(value = "修改商户基础资料", notes = "附件module：store")
    @PostMapping("/update")
    public ResponseData update(@Valid StoreVo storeVo) {
        Long saleId = ShiroUtil.loginUserId();
        storeService.updateStore(storeVo, saleId);
        return ResponseData.success();
    }

    /**
     * 商户信息分页
     * @param page
     * @param storeStatus
     * @param search
     * @param isDesc
     * @return
     */
    @ApiOperation(value = "商户信息分页")
    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeStatus", value = "类型：  create|新商户，follow|跟进中， wait|等待审核，success|申请成功,failure|申请失败"),
            @ApiImplicitParam(name = "search", value = "搜索"),
            @ApiImplicitParam(name = "isDesc", value = "排序：true倒叙", defaultValue = "true")
    })
    public ResponseData page(Page<StoreVo> page, StoreStatus storeStatus, String search, Boolean isDesc) {
        Page<StoreVo> iPage = storeService.findPage(page, storeStatus, ShiroUtil.loginUserId(), search, isDesc);
        return ResponseData.success(iPage);
    }



}
